import http from '@ohos.net.http';
import data_storage from '@ohos.data.storage';
import featureAbility from '@ohos.ability.featureAbility';
import router from '@system.router';

export default {
    data: {
        title: 'World'
    },
    submit(e) {
        //获取表单用户输入信息
        const username = e.value.username;
        const password = e.value.password;
        //请求服务器进行用户登录
        //发送http请求
        const httpRequest = http.createHttp();
        httpRequest.request("http://rwiwhu.natappfree.cc/edu-platform/background/api/studentLogin", {
            method: "POST",
            extraData:{
                "username":username,
                "password":password
            }
        },(err, data) => {
            if (!err) {
                console.info('Result:' + data.result);
                console.info('code:' + data.responseCode);
                //TOKEN存取
                const result = JSON.parse(data.result);
                const token = result.data.token;
                var context = featureAbility.getContext();
                context.getFilesDir().then((filePath) => {
                    console.info("getFilesDirSuccess:"+filePath);
                    console.info("token:"+token);
                    let storage = data_storage.getStorageSync(filePath + '/mystore')
                    storage.putSync('token', token);
                    storage.flushSync()
                    //跳转到首页
                    router.push({
                        uri: "pages/index/index"
                    })
                });
            } else {
                console.info('error:' + JSON.stringify(err));
            }
        })
    }
}
